package org.wevil.util;

import java.time.LocalDate;
import java.time.Period;

/**
 * 身份证工具类
 *
 * @author wanglei
 * @since 2024-04-07
 */
public class IdcardUtil {

    /**
     * 年龄
     * */
    public static Integer getAge(String idcard){
        LocalDate localDate = LocalDate.of(
                Integer.parseInt(idcard.substring(6, 10)),
                Integer.parseInt(idcard.substring(10, 12)),
                Integer.parseInt(idcard.substring(12, 14))
        );
        // 使用Period.between计算两个日期之间的差异
        Period period = Period.between(localDate, LocalDate.now());
        // 返回年份差
        return period.getYears();
    }

    /** 根据证件号码获取出生日期 */
    public static String getBirthday(String idcard){
        return idcard.substring(6, 10) + "-" + idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
    }

    /** 根据证件号码获取性别 */
    public static String getSex(String idcard){
        int sexInt = Integer.parseInt(idcard.substring(16, 17)) % 2 == 0 ? 2 : 1;
        if(sexInt == 2){
            return "女";
        }
        else{
            return "男";
        }
    }

    /** 根据证件号码获取性别 1男 2女 */
    public static Integer getSexInt(String idcard){
        return Integer.parseInt(idcard.substring(16, 17)) % 2 == 0 ? 2 : 1;
    }
    
}
